//
//  VideoRnView.m
//  xmgjAppFront
//
//  Created by ZekiMini on 2020/9/16.
//  Copyright © 2020 guojutech. All rights reserved.
//

#import "VideoRnView.h"
#import <React/RCTUIManager.h>
#import <React/RCTDefines.h>
#import "GJVideoPreviewView.h"
@interface VideoRnView ()
@property (nonatomic, strong)GJVideoPreviewView *videoView;
@end

@implementation VideoRnView
//标记宏
RCT_EXPORT_MODULE()

#pragma mark -- method
//开始播放
RCT_EXPORT_METHOD(play:(nonnull NSNumber *)reactTag path:(NSDictionary *)path){
  [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, GJVideoPreviewView *> *viewRegistry) {
    GJVideoPreviewView *view = viewRegistry[reactTag];
    if (![view isKindOfClass:[GJVideoPreviewView class]]) {
      //      RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
    } else {
      //播放
      dispatch_async(dispatch_get_main_queue(), ^{
        [self.videoView playWithPath:path[@"path"]];
      });
    }
  }];
  
}
//重新播放
RCT_EXPORT_METHOD(resume:(nonnull NSNumber *)reactTag){
  [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, GJVideoPreviewView *> *viewRegistry) {
    GJVideoPreviewView *view = viewRegistry[reactTag];
    if (![view isKindOfClass:[GJVideoPreviewView class]]) {
      //      RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
    } else {
      //播放
      dispatch_async(dispatch_get_main_queue(), ^{
        [self.videoView resume];
      });
    }
  }];
}
//暂停播放
RCT_EXPORT_METHOD(pause:(nonnull NSNumber *)reactTag){
  [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, GJVideoPreviewView *> *viewRegistry) {
    GJVideoPreviewView *view = viewRegistry[reactTag];
    if (![view isKindOfClass:[GJVideoPreviewView class]]) {
      //      RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
    } else {
      dispatch_async(dispatch_get_main_queue(), ^{
        [self.videoView pause];
      });
    }
  }];
}


#pragma mark - getter
- (UIView *)view{
  return self.videoView;
}
- (GJVideoPreviewView *)videoView{
  if (!_videoView) {
    _videoView = [GJVideoPreviewView new];
  }
  return _videoView;
}
@end
